package com.itheima.tlias.web.controller;

import com.aliyun.boot.template.OssTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2024-02-01 09:10:16
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private OssTemplate ossTemplate;

    /**
     * 上传文件要求：
     *      前端：1、post请求   2、前端需要指定有文件上传的标记  enctype="multipart/form-data"   3、要指定上传的文件名称的name值
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile imageFile) throws IOException {
        String filename = imageFile.getOriginalFilename();   //上传的文件名称
        InputStream in = imageFile.getInputStream();  //上传文件对应的输入流
        return ossTemplate.upload(filename, in);
    }

}